<?php

namespace App\Model;

use Illuminate\Support\Facades\DB;

class TemplateBlock extends BaseModel
{
    protected $table = 'template_block';
    private $fields = [];


    public function getList($request)
    {
        $layout_id = $request['layout_id'] ?? 1;
        $sql = "select * from zb_template_block b left join zb_template_block_style l on b.style_id = l.style_id left join zb_template_block_source s on b.source_id = s.source_id WHERE b.layout_id = $layout_id ORDER BY sort_number";
        return DB::connection('api')->select($sql);
    }

    public function createLayout($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/template/layout/create', $data, 'urlencoded', 'GET');
    }


    public function selectLayout($data)
    {
        $d['layout_id'] = $data['layout_id'];
        $d['view_id'] = intval($data['view_id']);
        return curl_request(getKkaudioApiUrl() . '/inner/template/view/update', $d, 'urlencoded', 'GET');
    }

    public function layoutDel($data)
    {
        $d['layout_id'] = $data['layout_id'];
        return curl_request(getKkaudioApiUrl() . '/inner/template/layout/remove', $d, 'urlencoded', 'GET');
    }

    public function create($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/template/block/updateOrInsert', $data);
    }

    public function getBlock($request)
    {
        $where['block_id'] = $request['block_id'] ?? 58;
        return DB::connection('api')->table($this->table)->where($where)->first();
    }

    public function getBlockType($request)
    {
        $sql = "SELECT * from zb_template_block_type";
        return DB::connection('api')->select($sql);
    }

    public function getBlockStyle($request)
    {
        $type_id = $request['type_id'] ?? 0;
        $sql = "SELECT * from zb_template_block_style WHERE type_id = $type_id";
        return DB::connection('api')->select($sql);
    }

    public function getBlockSource($request)
    {
        $type_id = $request['type_id'] ?? 1;
        $sql = "SELECT * from zb_template_block_source WHERE type_id = $type_id";
        return DB::connection('api')->select($sql);
    }

    public function getBlockUp($sort_number, $layout_id)
    {
        $sql = "SELECT block_id,sort_number FROM zb_template_block WHERE layout_id = $layout_id AND sort_number < $sort_number ORDER BY sort_number DESC limit 1";
        return DB::connection('api')->select($sql);
    }

    public function getBlockDown($layout_id, $sort_number)
    {
        $sql = "SELECT block_id,sort_number FROM zb_template_block WHERE sort_number > $sort_number AND layout_id = $layout_id ORDER BY sort_number ASC limit 1";
        return DB::connection('api')->select($sql);
    }

    public function updateBlock($data)
    {
        if (!$data['block_id']) {
            return false;
        }
        return curl_request(getKkaudioApiUrl() . '/inner/template/block/sort', $data, 'urlencoded', 'GET');
    }

    public function delBlock($data)
    {
        if (!$data['block_id']) {
            return false;
        }
        return curl_request(getKkaudioApiUrl() . '/inner/template/block/remove', $data, 'urlencoded', 'GET');
    }

    public function getBannerBlock()
    {
        $where['type_id'] = 0;
        $where['style_id'] = 1;
        return DB::connection('api')->table($this->table)->where($where)->get()->toArray();
    }

}

